package com.taikang.hmp.hbusiness.diabetes.measure.weight.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.hbusiness.diabetes.measure.weight.bean.Weight;
import com.taikang.hmp.hbusiness.diabetes.measure.weight.service.IWeightService;
import com.taikang.hmp.hbusiness.diabetes.measure.weight.service.WeightUtil;

/**
 * 体重 author:qrp time:2015年8月5日 上午10:57:22
 */
public class WeightServiceImpl extends BaseServiceImpl implements
		IWeightService {

	public String saveWeight(Dto paramDto)  {
		Weight weight = new Weight();
		weight.setCustomer_id(paramDto.getAsString("customerid"));
		String date = paramDto.getAsString("date");// 执行时间
		weight.setDate(date);
		String value = paramDto.getAsString("value");
		weight.setValue(value);
		String unit = paramDto.getAsString("unit");
//		if (unit.equals("null")) {//完全没有作用，不需要判断
//			unit = "";
//		}
		weight.setUnit(unit);
		weight.setOperate_date(new Date());
		String record_id = java.util.UUID.randomUUID().toString();
		weight.setRecord_id(record_id);
		weight.setSource_cd(paramDto.getAsString("requestsource"));
		weight.setOperater(paramDto.getAsString("uid"));
		List<Weight> list = super.getNewDao().queryForList("weight_space.getWeightByDates", weight);
		if(list!=null && list.size()>0){
			super.getNewDao().update("weight_space.update_weight", weight);
		}else{
			super.getNewDao().insert("weight_space.insert_weight", weight);
		}
		
		// 返回评判结果
		String height = paramDto.getAsString("height");
		if(height!=null && !"".equals(height)){
			float height_2 = Float.parseFloat(height) * Float.parseFloat(height)
					/ 10000;
			float bmi = Float.parseFloat(value) / height_2;
			
			String weightWords = WeightUtil.getWeightWords(bmi);
			return weightWords;
		}else{
			return null;
		}
		
	}

	public List<Dto> getWeightList(Dto paramDto) {
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("customerid", paramDto.getAsString("customerid"));
		/*if (paramDto.getAsString("startdate").equals("<null>") || paramDto.getAsString("startdate").equals("")) {
			condition.put("start_date", "1900-01-01 00:00");
		} else {*/
			condition.put("start_date", paramDto.getAsString("startdate")+" 00:00");
		//}
		condition.put("end_date", paramDto.getAsString("enddate")+" 23:59");
		String height = paramDto.getAsString("height");
		float height_2 = Float.parseFloat(height) * Float.parseFloat(height) / 10000;
		List<Weight> info = super.getNewDao().queryForList("weight_space.getWeightList",condition);
		List<Dto> result = new ArrayList<Dto>();
		for (int i = 0; i < info.size(); i++) {
			Dto resultMap = new BaseDto();
			resultMap.put("date", info.get(i).getDate().substring(0,16));
			resultMap.put("value", info.get(i).getValue());
			resultMap.put("unit", info.get(i).getUnit());
			Float bmi = Float.parseFloat(info.get(i).getValue()) / height_2;
			DecimalFormat   fnum   =   new   DecimalFormat("##0.00");  
			
			resultMap.put("bmi", fnum.format(bmi));
			result.add(resultMap);
		}
		return result;
	}

	@Override
	public List<Weight> getWeightChartList(Dto paramDto) {
		Weight weight = new Weight();
		weight.setCustomer_id(paramDto.getAsString("customerid"));
		if ("1".equals(paramDto.getAsString("day"))) {
			weight.setBef_date(paramDto.getAsString("date") + " 00:00");
			weight.setAft_date(paramDto.getAsString("date") + " 23:59");
		} else {
			weight.setBef_date(paramDto.getAsString("befdate") + " 00:00");
			weight.setAft_date(paramDto.getAsString("aftdate") + " 23:59");
		}
		// TODO Auto-generated method stub
		List<Weight> weightlist = super.getNewDao().queryForList("weight_space.getWeightChartList", weight);
        return weightlist;
	}

	@Override
	public String getHeight(Dto paramDto) {
		// TODO Auto-generated method stub
		List<CustomerInfo> customer = super.getNewDao().queryForList("user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String height = null;
		if (customer != null) {
			height = customer.get(0).getHeight();
		}
		return height;
	}

}
